package com.deer.wms.base.system.web.bill;

import com.deer.wms.base.system.model.bill.*;
import com.deer.wms.base.system.model.box.BoxItem;
import com.deer.wms.base.system.model.item.ItemInfo;
import com.deer.wms.base.system.model.task.TaskInfo;
import com.deer.wms.base.system.service.bill.IBillInDetailService;
import com.deer.wms.base.system.service.bill.IBillInMasterService;
import com.deer.wms.base.system.service.bill.WorkMasterService;
import com.deer.wms.base.system.service.box.IBoxItemService;
import com.deer.wms.base.system.service.item.IItemInfoService;
import com.deer.wms.base.system.service.task.BillInTaskInfoService;
import com.deer.wms.base.system.service.task.ITaskInfoService;
import com.deer.wms.common.annotation.Log;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.core.page.TableDataInfo;
import com.deer.wms.common.core.result.Result;
import com.deer.wms.common.core.result.ResultGenerator;
import com.deer.wms.common.enums.BusinessType;
import com.deer.wms.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/in/workMaster")
public class workMasterController extends BaseController {

    private String prefix = "in/workMaster";
    @Autowired
    private WorkMasterService workMasterService;
    @Autowired
    private IBillInMasterService billInMasterService;

    @Autowired
    private BillInTaskInfoService billInTaskInfoService;

    @Autowired
    private IBillInDetailService billInDetailService;
    @Autowired
    private IItemInfoService itemInfoService;
    @Autowired
    private IBoxItemService boxItemService;


    @RequiresPermissions("in:workMaster:view")
    @GetMapping()
    public String workMaster()
    {
        return prefix + "/workMaster";
    }


    @RequiresPermissions("in:workMaster:list")
    @PostMapping("/findList")
    @ResponseBody
    public TableDataInfo findList(BillInMasterCriteria billInMasterCriteria)
    {
        startPage();
        List<BillInMasterDto> list = workMasterService.findList(billInMasterCriteria);
        return getDataTable(list);
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo findList(BillInMaster billInMaster)
    {
        startPage();
        List<BillInMaster> list = workMasterService.WorkList(billInMaster);
        return getDataTable(list);
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public String detail() {
        return prefix + "/detail";
    }

    /**
     * 作业表查询单个入库单详细信息
     * @param billId
     * @return
     */
    @RequestMapping("/findBillInMasterById")
    @ResponseBody
    public BillInMasterDto findBillInMasterById(Integer billId){
        return  billInMasterService.findBillInMasterById(billId);
    }

    /**
     * 作业表显示入库作业对应的物料明细
     * @param billId
     * @return
     */
    @PostMapping("/findBillInDetailListByBillId")
    @ResponseBody
    public TableDataInfo findBillInDetailListByBillId(Integer billId){
        startPage();
        List<BillInDetailDto> list = billInDetailService.findBillInDetailListByBillId(billId);
        return getDataTable(list);
    }

    /**
     * 作业表显示入库作业对应的物料明细
     * @param billId
     * @return
     */
    @PostMapping("/findBillInDetailListsByBillId")
    @ResponseBody
    public TableDataInfo findBillInDetailListsByBillId(Integer billId){
        startPage();
        List<BillInDetailDto> list = billInDetailService.findBillInDetailListsByBillId(billId);
        return getDataTable(list);
    }

    /**
     * 在入库单作业列表显示任务列表
     * @param billId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTaskinfoByBillId")
    public TableDataInfo selectTaskinfoByBillId(Integer billId) {
        startPage();
        List<TaskInfo>  taskInfoList = billInTaskInfoService.selectTaskInfoBybillId(billId);
        return getDataTable(taskInfoList);
    }



    /**
     * 修改入库作业组盘数据
     * @param taskInfo
     * @return
     */
    @RequestMapping("/updateZuPan")
    @ResponseBody
    public AjaxResult updateZuPan(TaskInfo taskInfo){

        return toAjax(billInTaskInfoService.updateZuPan(taskInfo));
    }

    /**
     * 开始作业,状态变为3,即正在入库
     * @param billId
     * @return
     */
    @RequestMapping("/startAssignment")
    @ResponseBody
    public AjaxResult startAssignment(Integer billId){
        return toAjax(billInMasterService.startAssignment(billId));
    }

    /**
     * 取消作业,状态变为2
     * @param billId
     * @return
     */
    @RequestMapping("/endAssignment")
    @ResponseBody
    public AjaxResult endAssignment(Integer billId){
        return toAjax(billInMasterService.endAssignment(billId));
    }

    /**
     * 修改入库作业
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer billId, ModelMap mmap) {
        BillInMasterDto billInMaster = billInMasterService.selectBillInMasterById(billId);
        mmap.put("billInMaster", billInMaster);
        List<BillInDetailDto> billInDetailDtos = billInDetailService.findBillInDetailListByBillId(billInMaster.getBillId());
        mmap.put("billInDetails", billInDetailDtos);
        for (BillInDetail billInDetail: billInDetailDtos) {
            ItemInfo itemInfo = itemInfoService.selectItemInfoById(billInDetail.getItemId());
            mmap.put("itemInfo",itemInfo);
        }
        return prefix + "/edit";
    }

    /**
     * 修改保存入库作业信息
     */
    @RequiresPermissions("in:workMaster:edit")
    @Log(title = "入库作业", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public Result editSave(@RequestBody InserData inserData) {
        String createUserName = ShiroUtils.getLoginName();
        Integer userId = ShiroUtils.getUserId();
        BillInMaster billInMaster = inserData.getBillInMaster();
        billInMaster.setCreateUserName(createUserName);
        billInMaster.setCreateUserId(userId);
        billInMasterService.updateBillInMaster(billInMaster);
        //保存入库详情数据(物料信息)
        List<BillInDetailDto> billInDetails = inserData.getBillInDetailList();
        billInDetailService.deleteBillInDetailByBillId(billInMaster.getBillId());
        for (BillInDetail billInDetail : billInDetails) {
            billInDetail.setBillId(billInMaster.getBillId());
            billInDetailService.saveBillInDetail(billInDetail);
            BoxItem boxItem = new BoxItem();
            boxItem.setBillInDetailId(billInDetail.getBillInDetailId());
            boxItem.setBoxCode(billInDetail.getBoxCode());
            boxItem.setBatch(billInDetail.getBatch());
            boxItemService.insertBoxItem(boxItem);
        }
        return ResultGenerator.genSuccessResult();
    }

}
